package com.bsh.serviceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import com.bsh.api.params.DtoTransferBalance;
import com.bsh.dto.UserListQryDto;
import com.bsh.mybatis.dataView.UserAccountInfo;
import com.bsh.mybatis.model.Account;
import com.bsh.mybatis.model.AccountChangeAudit;
import com.bsh.mybatis.model.AccountNotice;
import com.bsh.mybatis.model.AccountRefundWithBLOBs;
import com.bsh.mybatis.model.Partner;
import com.bsh.mybatis.model.PartnerLevel;
import com.bsh.mybatis.model.Payment;
import com.bsh.mybatis.model.User;
import com.bsh.mybatis.model.UserBank;
import com.bsh.repository.DaoAccount;
import com.bsh.repository.DaoAccountChangeAudit;
import com.bsh.repository.DaoAccountLog;
import com.bsh.repository.DaoAccountNotice;
import com.bsh.repository.DaoAccountRefund;
import com.bsh.repository.DaoPartner;
import com.bsh.repository.DaoPartnerLevel;
import com.bsh.repository.DaoPayment;
import com.bsh.repository.DaoUser;
import com.bsh.service.AccountService;
import com.bsh.service.RecommendService;
import com.bsh.service.UpgradeService;
import com.bsh.service.UserBankService;
import com.bsh.utils.Pager;
import com.bsh.utils.PrimaryGenerater;
import com.bsh.utils.system.AccountLock;

/**
 * 账户
 * 
 * @author gsh
 *
 */
@Service
@Transactional
public class AccountServiceImpl implements AccountService {
	private static Logger log = LoggerFactory.getLogger(AccountServiceImpl.class);
	@Autowired
	private DaoAccount daoAcc;
	@Autowired
	private DaoUser daoUser;
	@Autowired
	DaoAccountNotice daoNotice;
	@Autowired
	DaoAccountLog daoAccLog;
	@Autowired
	DaoAccountRefund daoAccRefund;
	@Autowired
	RecommendService serRecomm;
	@Autowired
	DaoPayment daoPay;
	@Autowired
	UpgradeService upgradeSer;
	@Autowired
	UserBankService userBankServ;
	@Autowired
	DaoPartnerLevel daoPartnerLevel;
	@Autowired
	DaoPartner daoPart;
	@Autowired
	RecommendService recommendSer;
	@Autowired
	DaoAccountChangeAudit daoChangeAudit;

	/**
	 * 取得用户账户信息
	 */
	@Override
	public UserAccountInfo getUserAccountInfo(int userid) {
		UserAccountInfo userAcc = null;
		User user = daoUser.selectByPrimaryKey(userid);
		if (user == null)
			return userAcc;

		userAcc = new UserAccountInfo();
		userAcc.setName(user.getName());
		userAcc.setEnabled(user.getIsEffect());
		userAcc.setUserid(user.getId());

		Account acc = daoAcc.getUserAccount(userid);
		BigDecimal money = null, cloudMoney = null;
		if (acc != null) {
			money = acc.getMoney() == null ? BigDecimal.ZERO : acc.getMoney();
			cloudMoney = acc.getCloudMoney() == null ? BigDecimal.ZERO : acc.getCloudMoney();
			userAcc.setAccountId(acc.getId());
		}
		if (acc == null) {
			money = cloudMoney = BigDecimal.ZERO;
		}
		userAcc.setMoney(money);
		userAcc.setCloudMoney(cloudMoney);
		return userAcc;
	}

	/**
	 * 审核后台直接修改资金
	 * 
	 * @param changeId
	 * @param auditorId
	 * @param isPass
	 * @return -1账户不存在，0:未处理，-100 账户资金余额不足 -101 账户商币余额不足 ,大于0成功
	 */
	@Override
	public String auditAccountChange(Integer changeId, Integer auditorId, boolean isPass) {
		StringBuilder sb = new StringBuilder();
		if (isPass) {
			AccountChangeAudit changeInfo = daoChangeAudit.selectByPrimaryKey(changeId);
			Integer res1 = 0, res2 = 0;

			UserAccountInfo currAccInfo = this.getUserAccountInfo(changeInfo.getUserid());
			BigDecimal changeMoney = changeInfo.getMoney();
			BigDecimal changeCloudMoney = changeInfo.getCloudMoney();

			// 减现金，账户无钱
			if (changeMoney != null && changeMoney.compareTo(BigDecimal.ZERO) <= 0
					&& (changeInfo == null || changeInfo.getMoney() == null)) {
				return "err:保存失败！账户不存在或资金不足。";
			}

			// 减商币，账户无商币
			if (changeCloudMoney != null && changeCloudMoney.compareTo(BigDecimal.ZERO) <= 0
					&& (changeInfo == null || changeInfo.getCloudMoney() == null)) {
				return "err:保存失败！账户不存在或商币不足。";
			}

			// 扣钱超出余额
			if (changeMoney != null && changeMoney.compareTo(BigDecimal.ZERO) <= 0
					&& changeMoney.abs().compareTo(currAccInfo.getMoney()) > 0) {
				return "err:保存失败！账户资金余额不足。";
			}

			// 扣商币超出余额
			if (changeCloudMoney != null && changeCloudMoney.compareTo(BigDecimal.ZERO) <= 0
					&& changeCloudMoney.abs().compareTo(currAccInfo.getCloudMoney()) > 0) {
				return "err:保存失败！账户商币余额不足。";
			}

			// 修改资金
			res1 = editAccoutnMoney(changeInfo);
			res2 = editAccoutnCloudMoney(changeInfo);
			// res1: -100 账户资金余额不足,-1账户不存在，0:未处理
			// res2: -101 账户商币余额不足,-102没有选择加币类型,-1账户不存在，0:未处理

			if (res1 == -1 || res2 == -1)
				sb.append(",账户不存在");
			if (res1 == -100)
				sb.append(",账户资金余额不足");
			if (res2 == -101)
				sb.append(",账户商币余额不足");
			if (res2 == -102)
				sb.append(",该用户是商家，没有加币类型");

			if (res1 > 0 || res2 > 0) {
				daoChangeAudit.auditPass(changeId, auditorId);
			} else {
				// 审核记录未执行的话，回滚
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				String msg = sb.toString();
				if (msg.startsWith(","))
					msg = msg.replaceFirst(",", "");
				return "err:保存失败！" + msg + "。";
			}

		}
		if (!isPass) {
			daoChangeAudit.auditRefuse(changeId, auditorId);
		}
		return "ok:保存成功";
	}

	/**
	 * 后台直接加现金
	 * 
	 * @param accChangeInfo
	 *            用户账户变动信息
	 * @param adminId
	 *            操作管理员id
	 * @param cloudMoneyType
	 *            0:收款 1:消费
	 * @return -100 账户资金余额不足 -1账户不存在，0:未处理
	 */
	@Override
	public Integer editAccoutnMoney(AccountChangeAudit changeInfo) {
		Integer res = 0;
		Integer accountId = changeInfo.getAccountid();
		Integer userid = changeInfo.getUserid();

		// 账户初使化
		if (accountId == null) {
			accountId = daoAcc.initUserAcount(userid);
		}

		// 没有查询到用户的帐号,退出
		if (daoAcc.selectByPrimaryKey(accountId) == null)
			return -1;

		// 读取用户账户信息
		UserAccountInfo currAccInfo = this.getUserAccountInfo(userid);
		// 加现金
		if (changeInfo.getMoney() != null && changeInfo.getMoney().compareTo(BigDecimal.ZERO) != 0) {
			// 如果是扣钱，判断余额
			if (changeInfo.getMoney().compareTo(BigDecimal.ZERO) < 0) {
				if (currAccInfo.getMoney() == null
						|| currAccInfo.getMoney().compareTo(changeInfo.getMoney().abs()) < 0) {
					return -100;
				}
			}

			String sn = PrimaryGenerater.getInstance().next();

			// 账户变动前记录日志
			daoAccLog.addLog(accountId, sn, changeInfo.getSubmitterIp());

			// 加现金
			res = daoAcc.addMoney(accountId, userid, changeInfo.getMoney());

			// 读取用户账户信息
			UserAccountInfo endAccInfo = this.getUserAccountInfo(userid);

			// 写资金日志明细
			AccountNotice notice = new AccountNotice();
			notice.setAccountId(accountId);
			notice.setUserid(userid);
			notice.setMoney(changeInfo.getMoney());// 现金变动

			notice.setTradeType(7); // 管理员现金充值
			notice.setSn(sn);// 流水号
			notice.setOuterTime(new Date());
			notice.setOperator(changeInfo.getSubmitter());// 操作人

			notice.setTitle(String.format("后台转帐: %s 元,操作人:%s", changeInfo.getMoney(), changeInfo.getSubmitterName()));
			notice.setMemo(changeInfo.getMemo());
			notice.setPaymentId(1);// 管理员充值类型
			notice.setIp(changeInfo.getSubmitterIp());
			notice.setIsPaid((byte) 1);// 已付款
			notice.setMoneyType((short) 1);// 现金类型
			notice.setCreateTime(new Date());
			notice.setMoneyDown(endAccInfo.getMoney()); // 结存现金余额
			notice.setCloudMoneyDown(endAccInfo.getCloudMoney()); // 结存商币余额
			daoNotice.insert(notice);
		}
		return res;
	}

	/**
	 * 后台直接加商币
	 * 
	 * @param accChangeInfo
	 *            用户账户变动信息
	 * @param adminId
	 *            操作管理员id
	 * @param cloudMoneyType
	 *            0:收款 1:消费
	 * @return -101 账户商币余额不足,-102没有选择加币类型,-1账户不存在，0:未处理
	 */
	@Override
	public Integer editAccoutnCloudMoney(AccountChangeAudit changeInfo) {
		Integer res = 0;
		Integer accountId = changeInfo.getAccountid();
		Integer userid = changeInfo.getUserid();

		// 账户初使化
		if (accountId == null) {
			accountId = daoAcc.initUserAcount(userid);
		}

		// 没有查询到用户的帐号,退出
		if (daoAcc.selectByPrimaryKey(accountId) == null)
			return -1;

		// 读取用户账户信息
		UserAccountInfo currAccInfo = this.getUserAccountInfo(userid);
		// 加商币
		if (changeInfo.getCloudMoney() != null && changeInfo.getCloudMoney().compareTo(BigDecimal.ZERO) != 0) {
			// 加商币时，要检测商家收款或消费返币，向推荐人或合伙人返币
			if (changeInfo.getCloudMoney() != null && changeInfo.getCloudMoney().compareTo(BigDecimal.ZERO) > 0) {
				// 操作
				String sn = PrimaryGenerater.getInstance().next();
				// 账户变动前记录日志
				daoAccLog.addLog(accountId, sn, changeInfo.getSubmitterIp());
				// 加商币
				res = daoAcc.addCloudMoney(accountId, userid, changeInfo.getCloudMoney());
				// 读取用户账户信息
				UserAccountInfo endAccInfo = this.getUserAccountInfo(userid);
				// 写商币日志明细
				AccountNotice notice = new AccountNotice();
				notice.setAccountId(accountId);
				notice.setUserid(userid);
				notice.setCloudMoney(changeInfo.getCloudMoney());// 商币

				notice.setTradeType(8); // 管理员商币充值
				notice.setSn(sn);// 流水号
				notice.setOuterTime(new Date());
				notice.setOperator(changeInfo.getSubmitter());// 操作人
				notice.setTitle(
						String.format("后台转帐: %s 商币,操作人:%s", changeInfo.getCloudMoney(), changeInfo.getSubmitterName()));
				notice.setMemo(changeInfo.getMemo());
				notice.setPaymentId(1);// 管理员充值类型
				notice.setIp(changeInfo.getSubmitterIp());
				notice.setIsPaid((byte) 1);// 已付款
				notice.setMoneyType((short) 0);// 商币类型
				notice.setCreateTime(new Date());
				notice.setMoneyDown(endAccInfo.getMoney()); // 结存现金余额
				notice.setCloudMoneyDown(endAccInfo.getCloudMoney()); // 结存商币余额
				daoNotice.insert(notice);
				// if(changeInfo.getCloudmoneytype() !=null) cloudMoneyType =
				// changeInfo.getCloudmoneytype();
				String type = "";
				String actionType = "";
				actionType = changeInfo.getCloudmoneytype();
				if (res > 0) {
					// 推荐人翻商币
					User uInfo = daoUser.getUser(userid);

					if (uInfo.getPid() != null && uInfo.getPid() > 0) {
						BigDecimal fee = new BigDecimal("0.02");
						User pUinfo = daoUser.getUser(uInfo.getPid());
						if (pUinfo != null && pUinfo.getId() != null && pUinfo.getId() > 0) {
							if (uInfo.getIsMerchant() == 1) {
								// 商家
								if (changeInfo.getCloudmoneytype() != null) {
									switch (changeInfo.getCloudmoneytype()) {
									case "collection":
										// 收款行为
//										fee = new BigDecimal("0.02");
										type = "B";
										break;
									case "consumption":
										// 消费行为
//										fee = new BigDecimal("0.03");
										type = "C";
										break;
									}
									actionType = changeInfo.getCloudmoneytype();
									// 公式：( 收到商币总额 乘 100 除以 15 ) 乘 2% 只针对商家
									BigDecimal totalCloudMoney = changeInfo.getCloudMoney()
											.multiply(new BigDecimal("100"))
											.divide(new BigDecimal("15"), 2, BigDecimal.ROUND_HALF_DOWN);
									recommendSer.adminAddCloudMoney(totalCloudMoney, type, uInfo, sn, actionType);
								} else {
									// 主动回滚
									log.error("ADMIN_ERROR:主动回滚 后台加商币给的是商家，但没有选择加币类型");
									TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
									return -102;
								}
							} else {
								// 会员
								BigDecimal totalCloudMoney = changeInfo.getCloudMoney();
//								fee = new BigDecimal("0.03");
								type = "C";
								actionType = changeInfo.getCloudmoneytype();
								recommendSer.adminAddCloudMoney(totalCloudMoney, type, uInfo, sn, actionType);
							}
						}
					}

					// 合伙人返商币
					if (uInfo.getIsMerchant() == 1) {
						// 公式：( 收到商币总额 乘 100 除以 15 ) 乘 2% 只针对商家
						BigDecimal totalCloudMoneyPartner = changeInfo.getCloudMoney()
								.multiply(new BigDecimal("100"))
								.divide(new BigDecimal("15"), 2, BigDecimal.ROUND_HALF_DOWN);
						recommendSer.adminAddCloudMoneyPartner(totalCloudMoneyPartner, uInfo, sn, actionType);
					}

				}
			}
			// 扣商币动作
			else if (changeInfo.getCloudMoney() != null
					&& changeInfo.getCloudMoney().compareTo(BigDecimal.ZERO) == -1) {
				// 如果是扣商币，判断余额
				if (changeInfo.getCloudMoney().compareTo(BigDecimal.ZERO) < 0) {
					if (currAccInfo.getCloudMoney() == null
							|| currAccInfo.getCloudMoney().compareTo(changeInfo.getCloudMoney().abs()) < 0) {
						return -101;
					}
				}
				String sn = PrimaryGenerater.getInstance().next();

				// 账户变动前记录日志
				daoAccLog.addLog(accountId, sn, changeInfo.getCloudmoneytype());

				// 加商币
				res = daoAcc.addCloudMoney(accountId, userid, changeInfo.getCloudMoney());

				// 读取用户账户信息
				UserAccountInfo endAccInfo = this.getUserAccountInfo(userid);

				// 写商币日志明细
				AccountNotice notice = new AccountNotice();
				notice.setAccountId(accountId);
				notice.setUserid(userid);
				notice.setCloudMoney(changeInfo.getCloudMoney());// 商币

				notice.setTradeType(8); // 管理员商币充值
				notice.setSn(sn);// 流水号
				notice.setOuterTime(new Date());
				notice.setOperator(changeInfo.getSubmitter());// 操作人

				notice.setTitle(
						String.format("后台转帐: %s 商币,操作人:%s", changeInfo.getCloudMoney(), changeInfo.getSubmitterName()));
				notice.setMemo(changeInfo.getMemo());
				notice.setPaymentId(1);// 管理员充值类型
				notice.setIp(changeInfo.getSubmitterIp());
				notice.setIsPaid((byte) 1);// 已付款
				notice.setMoneyType((short) 0);// 商币类型
				notice.setCreateTime(new Date());
				notice.setMoneyDown(endAccInfo.getMoney()); // 结存现金余额
				notice.setCloudMoneyDown(endAccInfo.getCloudMoney()); // 结存商币余额
				daoNotice.insert(notice);
			}
		}
		return res;
	}

	/**
	 * 更新账户支付密码
	 */
	@Override
	public Integer editAccountPassword(Account accInfo) {

		Integer accountId = accInfo.getId();
		Integer userid = accInfo.getUserid();

		if (daoAcc.selectByPrimaryKey(accountId) == null) // 没有查询到用户的帐号,退出
			return -1;
		String newPassword = accInfo.getPayPassword();
		if (newPassword == null) {
			return -1;
		}
		Integer res = daoAcc.editAccountPassword(accountId, userid, newPassword);
		return res;
	}

	@Override
	public Account getAccountByUserId(int userid) {
		Account acc = daoAcc.getUserAccount(userid);
		return acc;
	}

	@Override
	public Integer financeTransferBalance(String userName, String toUserName, BigDecimal money, String ip,
			String memo) {
		Integer res = -1;
		if (money == null || money.compareTo(new BigDecimal("0.00")) == 0
				|| money.compareTo(new BigDecimal("0.00")) == -1) {
			return -2;
		}

		User userInfo = daoUser.selectByName(userName);
		User toUserInfo = daoUser.selectByName(toUserName);
		if (userInfo == null || toUserInfo == null) {
			return -3;
		}

		Account accInfo = daoAcc.getUserAccount(userInfo.getId());
		Account toAccInfo = daoAcc.getUserAccount(toUserInfo.getId());

		Integer accountId = accInfo.getId();
		Integer userid = userInfo.getId();

		Integer toAccountId = toAccInfo.getId();
		Integer toUserid = toUserInfo.getId();

		// 账户初使化
		if (accountId == null) {
			accountId = daoAcc.initUserAcount(userid);
		}
		// 账户初使化
		if (toAccountId == null) {
			toAccountId = daoAcc.initUserAcount(toUserid);
		}

		// 没有查询到用户的帐号,退出
		if (daoAcc.selectByPrimaryKey(accountId) == null)
			return -4;

		if (accInfo.getMoney().compareTo(money) == -1) {
			// 余额不足
			return -5;
		}
		
		//获取账务锁
		boolean isLock = AccountLock.getLock(accountId, userid);
		//boolean isLock2 = AccountLock.getLock(toAccountId, toUserid);
		if (!isLock) {
			return -11;
		}

		// 加钱
		if (money != null) {
			String sn = PrimaryGenerater.getInstance().next();

			// 账户变动前记录日志
			daoAccLog.addLog(accountId, sn, ip);
			daoAccLog.addLog(toAccountId, sn, ip);

			BigDecimal b1 = new BigDecimal("-1");
			BigDecimal c1 = new BigDecimal("0.15"); // cloudMoney币
			BigDecimal m2 = new BigDecimal("0.85"); // Money元

			BigDecimal addCloudMoney = money;
			// 改游戏规则 2016-07-27 jane
			// 如果转账的接收方不是商家 ，那么不能定义为消费行为。 不能全返 和返商币
			if (toUserInfo.getIsMerchant() ==null || toUserInfo.getIsMerchant() != 1) {
				// 如果收款方是普通会员 那么 p1这个商币乘0 ， 现金乘1
				c1 = new BigDecimal("0");
				m2 = new BigDecimal("1");
				addCloudMoney = BigDecimal.ZERO;
			}

			BigDecimal toUserMoney = money.multiply(m2);
			BigDecimal toUserCloudMoney = money.multiply(c1);
			toUserCloudMoney = toUserCloudMoney.setScale(2, BigDecimal.ROUND_HALF_UP);
			toUserMoney = toUserMoney.setScale(2, BigDecimal.ROUND_HALF_UP);

			// 转出者
			BigDecimal payUserMoney = money.multiply(b1); // 是负数
			res = daoAcc.addMoney(accountId, userid, payUserMoney);

			String snTemp = null;
			// 写资金日志明细 转出者
			AccountNotice notice = new AccountNotice();
			notice.setAccountId(accountId);
			notice.setUserid(userid);
			notice.setMoney(payUserMoney);// 现金

			notice.setTradeType(2); // 余额转帐
			notice.setSn(sn);// 唯一流水号
			notice.setOuterTime(new Date());

			notice.setTitle(String.format("余额转账给%s, -%s元 ", toUserInfo.getName(), money));
			notice.setMemo(memo);
			notice.setPaymentId(2);// 余额转账
			notice.setIp(ip);
			notice.setIsPaid((byte) 1);// 已付款
			notice.setMoneyType((short) 1);// 现金类型
			notice.setCreateTime(new Date());
			notice.setToUserid(toUserid);
			// 在查
			Account downAccInfo = daoAcc.selectByPrimaryKey(accountId);
			// notice.setMoneyDown( accInfo.getMoney().subtract(money) );
			// //结存现金余额
			notice.setMoneyDown(downAccInfo.getMoney()); // 结存现金余额
			notice.setCloudMoneyDown(downAccInfo.getCloudMoney()); // 结存商币余额
			daoNotice.insert(notice);

			// 改游戏规则
			// 如果是消费行为 有全返商币时操作加商币
			if (addCloudMoney.compareTo(BigDecimal.ZERO) == 1) {
				res = daoAcc.addCloudMoney(accountId, userid, addCloudMoney);
				AccountNotice noticeCloud = new AccountNotice();
				noticeCloud.setAccountId(accountId);
				noticeCloud.setUserid(userid);
				noticeCloud.setCloudMoney(addCloudMoney);// 商币

				noticeCloud.setTradeType(2); // 余额转帐
				snTemp = PrimaryGenerater.getInstance().next();
				noticeCloud.setCsn(sn);// 这笔转账动作流水号
				noticeCloud.setSn(snTemp);// 唯一流水号
				noticeCloud.setOuterTime(new Date());

				noticeCloud.setTitle(String.format("余额转账给%s得商币+%s", toUserInfo.getName(), money));
				noticeCloud.setMemo(memo);
				noticeCloud.setPaymentId(2);// 余额转账
				noticeCloud.setIp(ip);
				noticeCloud.setIsPaid((byte) 1);// 已付款
				noticeCloud.setMoneyType((short) 0);// 现金类型
				noticeCloud.setCreateTime(new Date());
				// 在查
				Account downAccInfoCloud = daoAcc.selectByPrimaryKey(accountId);
				// noticeCloud.setMoneyDown( accInfo.getMoney().subtract(money)
				// ); //结存现金余额
				noticeCloud.setMoneyDown(downAccInfoCloud.getMoney()); // 结存现金余额
				noticeCloud.setCloudMoneyDown(downAccInfoCloud.getCloudMoney()); // 结存商币余额
				daoNotice.insert(noticeCloud);
			}

			// 接收者
			res = daoAcc.addMoney(toAccountId, toUserid, toUserMoney);

			// 写资金日志明细 转入者
			AccountNotice toNotice = new AccountNotice();
			toNotice.setAccountId(toAccountId);
			toNotice.setUserid(toUserid);
			toNotice.setMoney(toUserMoney);// 现金

			toNotice.setTradeType(2); // 余额转帐
			snTemp = PrimaryGenerater.getInstance().next();
			toNotice.setCsn(sn);// 这笔转账动作流水号
			toNotice.setSn(snTemp);// 唯一流水号
			toNotice.setOuterTime(new Date());

			toNotice.setTitle(String.format("%s余额转账 +%s元", userInfo.getName(), toUserMoney));
			toNotice.setMemo(memo);
			toNotice.setPaymentId(2);// 余额转账
			toNotice.setIp(ip);
			toNotice.setIsPaid((byte) 1);// 已付款
			toNotice.setMoneyType((short) 1);// 现金类型
			toNotice.setCreateTime(new Date());
			// 在查
			Account downToAccInfo = daoAcc.selectByPrimaryKey(toAccountId);
			// toNotice.setMoneyDown( toAccInfo.getMoney().add(toUserMoney) );
			// //结存现金余额
			toNotice.setMoneyDown(downToAccInfo.getMoney()); // 结存现金余额
			toNotice.setCloudMoneyDown(downToAccInfo.getCloudMoney()); // 结存商币余额
			daoNotice.insert(toNotice);

			// 改游戏规则
			// 如果是消费行为 有返商币时操作加商币
			if (toUserCloudMoney.compareTo(BigDecimal.ZERO) == 1) {
				res = daoAcc.addCloudMoney(toAccountId, toUserid, toUserCloudMoney);
				AccountNotice toNoticeCloud = new AccountNotice();
				toNoticeCloud.setAccountId(toAccountId);
				toNoticeCloud.setUserid(toUserid);
				toNoticeCloud.setCloudMoney(toUserCloudMoney);// 商币

				toNoticeCloud.setTradeType(2); // 余额转帐
				snTemp = PrimaryGenerater.getInstance().next();
				toNoticeCloud.setCsn(sn);// 这笔转账动作流水号
				toNoticeCloud.setSn(snTemp);// 唯一流水号
				toNoticeCloud.setOuterTime(new Date());

				toNoticeCloud.setTitle(String.format("%s余额转账 得商币+%s", userInfo.getName(), toUserCloudMoney));
				toNoticeCloud.setMemo(memo);
				toNoticeCloud.setPaymentId(2);// 余额转账
				toNoticeCloud.setIp(ip);
				toNoticeCloud.setIsPaid((byte) 1);// 已付款
				toNoticeCloud.setMoneyType((short) 0);// 商币类型
				toNoticeCloud.setCreateTime(new Date());
				// 在查
				Account downToAccInfoCloud = daoAcc.selectByPrimaryKey(toAccountId);
				// toNoticeCloud.setMoneyDown(
				// toAccInfo.getMoney().add(toUserMoney) ); //结存现金余额
				toNoticeCloud.setMoneyDown(downToAccInfoCloud.getMoney()); // 结存现金余额
				toNoticeCloud.setCloudMoneyDown(downToAccInfoCloud.getCloudMoney()); // 结存商币余额

				daoNotice.insert(toNoticeCloud);
			}

			// Account accLastCheck = daoAcc.getUserAccount(userInfo.getId());
			
			//AccountLock.unLock(toAccountId, toUserid);

			// if(accLastCheck.getMoney().compareTo(money) == -1){
			// //余额不足
			// //主动回滚
			// log.error("ERROR_IMP:余额转账主动回滚,余额不足["+userInfo.getName()+"]"+money+"-"+accLastCheck.getMoney());
			// System.out.println("ERROR_IMP:余额转账主动回滚,余额不足["+userInfo.getName()+"]"+money+"-"+accLastCheck.getMoney());
			//
			// TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			// return -7;
			// }

			// 改游戏规则 判断 是不是消费行为即 收款方是不是商家
			// 收款方是商家 让进入推荐返币和合伙人返币
			if (toUserInfo.getIsMerchant() == 1) {
				// 给上线推荐奖 和 合伙人返
				serRecomm.transferByCash(userid, toUserid, money, sn);
			}

		}
		
		//解锁
		AccountLock.unLock(accountId, userid);
		return res;
	}

	/**
	 * 创建account用户，如果存在直接返回accountId
	 * 
	 * @param userid
	 * @return accountId
	 */
	@Override
	public Integer newAccountByUser(int userid) {
		// 账户初使化
		Integer accountId = daoAcc.initUserAcount(userid);
		return accountId;
	}

	/**
	 * 审核提现申请 注：不通过审核，要退回资金
	 * 
	 * @param user
	 * @return
	 */
	@Override
	public int auditCash(int askid, boolean isPass, String auditMsg, int adminId, String adminName, String ip) {
		int res = daoAccRefund.audit(askid, isPass, auditMsg);
		// 如果未审核通过，退回资金
		if (isPass == false && res > 0) {

			AccountRefundWithBLOBs accRef = daoAccRefund.selectByPrimaryKey(askid);

			// 取得用户账户信息
			UserAccountInfo accInfo = this.getUserAccountInfo(accRef.getUserid());

			Integer accountId = accInfo.getAccountId();
			Integer userid = accInfo.getUserid();

			// 账户初使化
			if (accountId == null) {
				accountId = daoAcc.initUserAcount(userid);
			}

			// 没有查询到用户的帐号,退出
			if (daoAcc.selectByPrimaryKey(accountId) == null)
				return -1;

			// 返回现金
			if (accInfo.getMoney() != null) {
				if (accRef.getMoney().compareTo(BigDecimal.ZERO) > 0) {
					res = daoAcc.addMoney(accountId, userid, accRef.getMoney());
					// 读取用户账户信息
					UserAccountInfo endAccInfo = this.getUserAccountInfo(userid);

					String sn = PrimaryGenerater.getInstance().next();
					// 写资金日志明细
					AccountNotice notice = new AccountNotice();
					notice.setAccountId(accountId);
					notice.setUserid(userid);
					notice.setMoney(accRef.getMoney());// 现金

					notice.setTradeType(3); // 用户提现
					notice.setSn(sn);// 流水号
					notice.setOuterTime(new Date());
					notice.setOperator(adminId);// 操作人

					notice.setTitle(String.format("提现失败,退回: %s 元,审核人:%s", accRef.getMoney(), adminName));
					notice.setMemo(accRef.getMemo());
					notice.setPaymentId(1);// 管理员充值类型
					notice.setIp(ip);
					notice.setIsPaid((byte) 1);// 已付款
					notice.setMoneyType((short) 1);// 现金类型
					notice.setCreateTime(new Date());
					notice.setMoneyDown(endAccInfo.getMoney()); // 结存现金余额
					notice.setCloudMoneyDown(endAccInfo.getCloudMoney()); // 结存商币余额
					daoNotice.insert(notice);
				}
			}
		}
		return res;
	}

	/**
	 * 撤消提现申请
	 * 
	 * @param user
	 * @return
	 */
	@Override
	public int auditCashCancel(int askid) {
		return daoAccRefund.auditCancel(askid);
	}

	/**
	 * 提现申请审核通过,确认打款 注意：提现申请前已扣过现金，这里不再扣除
	 * 
	 * @param user
	 * @return
	 */
	@Override
	public int auditPay(int askid, String payMsg, int adminId, String adminName, String ip) {
		// 审核通过
		Integer res = daoAccRefund.auditPay(askid, payMsg);
		if (res == null || res <= 0)
			return 0;

		AccountRefundWithBLOBs accRef = daoAccRefund.selectByPrimaryKey(askid);

		// 取得用户账户信息
		UserAccountInfo accInfo = this.getUserAccountInfo(accRef.getUserid());

		Integer accountId = accInfo.getAccountId();
		Integer userid = accInfo.getUserid();

		// 账户初使化
		if (accountId == null) {
			accountId = daoAcc.initUserAcount(userid);
		}

		// 没有查询到用户的帐号,退出
		if (daoAcc.selectByPrimaryKey(accountId) == null)
			return -1;

		// 扣除资金，写入帐单明细
		if (accInfo.getMoney() != null) {
			String sn = PrimaryGenerater.getInstance().next();

			// 提现申请前已扣过现金，这里不再扣除
			if (false) {
				// 账户变动前记录日志
				// daoAccLog.addLog(accountId, sn, ip);

				// 提取现金
				// res = daoAcc.addMoney(accountId, userid,
				// accRef.getMoney().abs().multiply(new BigDecimal("-1")));
			}

			// 提现前已写过帐单，这时不再写入
			if (false) {
				// 读取用户银行卡信息
				UserBank userBank = userBankServ.selectUserOneBankCard(accRef.getUserBankId(), userid);

				// 读取用户账户信息
				UserAccountInfo endAccInfo = this.getUserAccountInfo(userid);

				// 写资金日志明细
				AccountNotice notice = new AccountNotice();
				notice.setAccountId(accountId);
				notice.setUserid(userid);
				notice.setMoney(accRef.getMoney().abs().multiply(new BigDecimal("-1")));// 现金

				notice.setTradeType(3); // 用户提现
				notice.setSn(sn);// 流水号
				notice.setOuterTime(new Date());
				notice.setOperator(adminId);// 操作人

				notice.setBankCard(userBank.getBankCard());// 设置提现银行卡
				notice.setBankName(userBank.getBankName());// 设置提现银行卡开户行

				notice.setTitle(String.format("用户提现: %s 元,审核人:%s", accRef.getMoney(), adminName));
				notice.setMemo(accRef.getMemo());
				notice.setPaymentId(1);// 管理员充值类型
				notice.setIp(ip);
				notice.setIsPaid((byte) 1);// 已付款
				notice.setMoneyType((short) 1);// 现金类型
				notice.setCreateTime(new Date());
				notice.setMoneyDown(endAccInfo.getMoney()); // 结存现金余额
				notice.setCloudMoneyDown(endAccInfo.getCloudMoney()); // 结存商币余额
				daoNotice.insert(notice);
			}
		}

		return res;

	}

	/**
	 * 提现申请审核通过,确认打款 -- 批量
	 * 
	 * @param user
	 * @return 0失败 1全部成功 2部分成功，请检查数据状态
	 */
	@Override
	public int auditPayBatch(String askidList, String payMsg, int adminId, String adminName, String ip) {
		String[] ary = askidList.split("_");
		int ret = 0;
		StringBuilder sb = new StringBuilder();
		for (String askid : ary) {
			Integer askidVal = Integer.parseInt(askid);
			Integer res = auditPay(askidVal, payMsg, adminId, adminName, ip);

			int res2 = 0;
			if (res != null && res > 0)
				res2 = 1;

			sb.append("_" + Integer.toString(res2) + "_");
		}
		String retStr = sb.toString();
		if (retStr.contains("_0_") && retStr.contains("_1_"))
			ret = 2;
		else if (!retStr.contains("_0_") && retStr.contains("_1_"))
			ret = 1;
		return ret;
	}

	/**
	 * 创建account 重载形参带ip,
	 * 
	 * @return accountId
	 */
	@Override
	public Integer newAccountByUser(int userid, String ip) {
		// 账户初使化
		Integer accountId = daoAcc.initUserAcount(userid, ip);
		return accountId;
	}

	@Override
	@Transactional
	public Integer financeTransferOnlineWebHook(String sn, String outerSn, String resultCode) {
		Integer res = -1;
		AccountNotice an = new AccountNotice();
		an.setSn(sn);
		an = daoNotice.selectBySn(an);
		if (an == null || an.equals("")) {
			log.error("webHook_error:流水号数据库里没有[" + sn + "]");
			return -1;
		}
		if (an.getIsPaid() == 1) {
			log.error("webHook_info:数据库已经是isPaid[" + sn + "]");
			return 2;
		}
		boolean isLock1 = AccountLock.getLock(an.getAccountId(), an.getUserid());
		if (!isLock1) {
			return -11;
		}
		// 修改notice状态
		AccountNotice upAccNotice = new AccountNotice();
		upAccNotice.setOuterSn(outerSn);
		upAccNotice.setIsPaid(new Byte("1"));
		upAccNotice.setOuterTime(new Date());
		upAccNotice.setId(an.getId());
		res = daoNotice.updateByPrimaryKeySelective(upAccNotice);
		if (res > 0) {
			Payment upPaymentTotal = new Payment();
			upPaymentTotal.setId(an.getPaymentId());
			upPaymentTotal.setTotalAmount(an.getMoney());
			daoPay.updatePaymentTotal(upPaymentTotal);
			User userInfo = daoUser.selectByPrimaryKey(an.getUserid());
			User toUserInfo = daoUser.selectByPrimaryKey(an.getToUserid());
			if (userInfo == null || toUserInfo == null) {
				return -2;
			}
			Account accInfo = daoAcc.getUserAccount(userInfo.getId());
			Account toAccInfo = daoAcc.getUserAccount(toUserInfo.getId());
			Integer accountId = accInfo.getId();
			Integer userid = userInfo.getId();

			Integer toAccountId = toAccInfo.getId();
			Integer toUserid = toUserInfo.getId();

			// 账户初使化
			if (accountId == null) {
				accountId = daoAcc.initUserAcount(userid);
			}
			// 账户初使化
			if (toAccountId == null) {
				toAccountId = daoAcc.initUserAcount(toUserid);
			}

			// 没有查询到用户的帐号,退出
			if (daoAcc.selectByPrimaryKey(toAccountId) == null)
				return -3;

			BigDecimal money = an.getMoney();
			// 加钱
			if (money != null) {

				// 先给A加钱 在减钱， B加线
				// 把原先notice copy下， 在改原先 的sn改掉
				String ip = "222";
				// 账户变动前记录日志
				daoAccLog.addLog(accountId, sn, ip);
				daoAccLog.addLog(toAccountId, sn, ip);

				BigDecimal b1 = new BigDecimal("-1");
				BigDecimal c1 = new BigDecimal("0.15"); // cloudMoney币
				BigDecimal m2 = new BigDecimal("0.85"); // Money元

				BigDecimal addCloudMoney = money;
				// 改游戏规则 2016-07-27 jane
				// (08-01正式上传单个class 文件到生产) 基于在git
				// fde83e31e5f2d139f5894b5442d84943785367a0 版本修改
				// 如果转账的接收方不是商家 ，那么不能定义为消费行为。 不能全返 和返商币
				if (toUserInfo.getIsMerchant() != 1) {
					// 如果收款方是普通会员 那么 p1这个商币乘0 ， 现金乘1
					c1 = new BigDecimal("0");
					m2 = new BigDecimal("1");
					addCloudMoney = BigDecimal.ZERO;
				}

				BigDecimal toUserMoney = money.multiply(m2);
				BigDecimal toUserCloudMoney = money.multiply(c1);
				toUserCloudMoney = toUserCloudMoney.setScale(2, BigDecimal.ROUND_HALF_UP);
				toUserMoney = toUserMoney.setScale(2, BigDecimal.ROUND_HALF_UP);

				// 转出者
				BigDecimal payUserMoney = money.multiply(b1); // 是负数

				Payment payment = daoPay.getPaymentById(an.getPaymentId());

				String snTemp = null;
				// 写资金日志明细 转出者
				AccountNotice checkIsLast = new AccountNotice();
				checkIsLast.setId(an.getId());
				checkIsLast.setUserid(an.getUserid());
				AccountNotice checkRes = daoNotice.checkLastById(checkIsLast);
				// 在线支付回调 付款成功先加余额 +全额
				res = daoAcc.addMoney(accountId, userid, money);

				// 支付回调如果有时间延时 account_notice里的支付记录在其他交易之后
				if (checkRes == null) {
					// 如果是当前用户账单记录里的最后一条记录。 只更新 账单状态
					AccountNotice downAccNotice = new AccountNotice();
					Account downAccInfo = daoAcc.selectByPrimaryKey(accountId);
					downAccNotice.setMoneyDown(downAccInfo.getMoney()); // 结存现金余额
					downAccNotice.setCloudMoneyDown(downAccInfo.getCloudMoney()); // 结存商币余额
					downAccNotice.setId(an.getId());

					res = daoNotice.updateByPrimaryKeySelective(downAccNotice);
				} else {
					// copy一条记录 并把老记录的 isPaid改为-1
					String originSn = an.getSn();
					AccountNotice upOldAn = new AccountNotice();
					upOldAn.setId(an.getId());
					upOldAn.setSn(an.getSn() + "_old");
					upOldAn.setIsPaid(new Byte("-1")); // 老数据 isPaid是-1
					daoNotice.updateByPrimaryKeySelective(upOldAn);

					AccountNotice newAn = daoNotice.selectByPrimaryKey(an.getId());
					newAn.setId(null);
					newAn.setSn(originSn);
					newAn.setIsPaid(new Byte("1"));
					Account downAccInfo = daoAcc.selectByPrimaryKey(accountId);
					newAn.setMoneyDown(downAccInfo.getMoney()); // 结存现金余额
					newAn.setCloudMoneyDown(downAccInfo.getCloudMoney()); // 结存商币余额
					res = daoNotice.insertSelective(newAn);
				}

				// 在扣余额
				res = daoAcc.addMoney(accountId, userid, payUserMoney);
				AccountNotice notice = new AccountNotice();
				notice.setAccountId(accountId);
				notice.setUserid(userid);
				notice.setMoney(payUserMoney);// 现金负数

				notice.setTradeType(an.getTradeType()); // 在线转账
				snTemp = PrimaryGenerater.getInstance().next();
				notice.setCsn(sn);// 这笔转账动作流水号
				notice.setSn(snTemp);// 唯一流水号
				notice.setOuterTime(new Date());

				notice.setTitle(String.format("转账给%s, -%s元 ", toUserInfo.getName(), money));
				notice.setMemo(an.getMemo());
				notice.setPaymentId(an.getPaymentId());
				notice.setIp(an.getIp());
				notice.setIsPaid((byte) 1);// 已付款
				notice.setMoneyType((short) 1);// 现金类型
				notice.setCreateTime(new Date());
				notice.setToUserid(toUserid);
				// 在查
				Account downAccInfo = daoAcc.selectByPrimaryKey(accountId);
				notice.setMoneyDown(downAccInfo.getMoney()); // 结存现金余额
				notice.setCloudMoneyDown(downAccInfo.getCloudMoney()); // 结存商币余额
				daoNotice.insert(notice);

				// 改游戏规则
				// 如果是消费行为 有全返商币时操作加商币
				if (addCloudMoney.compareTo(BigDecimal.ZERO) == 1) {
					res = daoAcc.addCloudMoney(accountId, userid, addCloudMoney);
					AccountNotice noticeCloud = new AccountNotice();
					noticeCloud.setAccountId(accountId);
					noticeCloud.setUserid(userid);
					noticeCloud.setCloudMoney(addCloudMoney);// +商币

					noticeCloud.setTradeType(16); // 在线转帐
					snTemp = PrimaryGenerater.getInstance().next();
					noticeCloud.setCsn(sn);// 这笔转账动作流水号
					noticeCloud.setSn(snTemp);// 唯一流水号
					noticeCloud.setOuterTime(new Date());

					noticeCloud.setTitle(String.format("转账给%s得商币+%s", toUserInfo.getName(), addCloudMoney));
					noticeCloud.setMemo(an.getMemo());
					noticeCloud.setPaymentId(new Integer("3")); // 3 SysTransfer
																// 系统自动操作
					// noticeCloud.setOperator(new Integer("-1")); //系统操作
					noticeCloud.setIp(an.getIp());
					noticeCloud.setIsPaid((byte) 1);// 已付款
					noticeCloud.setMoneyType((short) 0);// 商币类型
					noticeCloud.setCreateTime(new Date());
					// 在查
					Account downAccInfoCloud = daoAcc.selectByPrimaryKey(accountId);
					noticeCloud.setMoneyDown(downAccInfoCloud.getMoney()); // 结存现金余额
					noticeCloud.setCloudMoneyDown(downAccInfoCloud.getCloudMoney()); // 结存商币余额
					daoNotice.insert(noticeCloud);
				}

				// 接收者
				res = daoAcc.addMoney(toAccountId, toUserid, toUserMoney);

				// 写资金日志明细 转入者
				AccountNotice toNotice = new AccountNotice();
				toNotice.setAccountId(toAccountId);
				toNotice.setUserid(toUserid);
				toNotice.setMoney(toUserMoney);// 现金

				toNotice.setTradeType(16); // 在线转帐
				snTemp = PrimaryGenerater.getInstance().next();
				toNotice.setCsn(sn);// 这笔转账动作流水号
				toNotice.setSn(snTemp);// 唯一流水号
				toNotice.setOuterTime(new Date());

				toNotice.setTitle(
						String.format("%s" + payment.getName() + "转账 ,+%s元", userInfo.getName(), toUserMoney));
				toNotice.setMemo(an.getMemo());
				toNotice.setPaymentId(an.getPaymentId());
				toNotice.setIp(an.getIp());
				toNotice.setIsPaid((byte) 1);// 已付款
				toNotice.setMoneyType((short) 1);// 现金类型
				toNotice.setCreateTime(new Date());
				// 在查
				Account downToAccInfo = daoAcc.selectByPrimaryKey(toAccountId);
				toNotice.setMoneyDown(downToAccInfo.getMoney()); // 结存现金余额
				toNotice.setCloudMoneyDown(downToAccInfo.getCloudMoney()); // 结存商币余额
				daoNotice.insert(toNotice);

				// 改游戏规则
				// 如果是消费行为 有返商币时操作加商币
				if (toUserCloudMoney.compareTo(BigDecimal.ZERO) == 1) {
					res = daoAcc.addCloudMoney(toAccountId, toUserid, toUserCloudMoney);
					AccountNotice toNoticeCloud = new AccountNotice();
					toNoticeCloud.setAccountId(toAccountId);
					toNoticeCloud.setUserid(toUserid);
					toNoticeCloud.setCloudMoney(toUserCloudMoney);// 商币

					toNoticeCloud.setTradeType(16); // 在线转帐
					snTemp = PrimaryGenerater.getInstance().next();
					toNoticeCloud.setCsn(sn);// 这笔转账动作流水号
					toNoticeCloud.setSn(snTemp);// 唯一流水号
					toNoticeCloud.setOuterTime(new Date());

					toNoticeCloud.setTitle(String.format("%s转账 得商币+%s", userInfo.getName(), toUserCloudMoney));
					toNoticeCloud.setMemo(an.getMemo());
					toNoticeCloud.setPaymentId(new Integer("3")); // 3
																	// SysTransfer
																	// 系统自动操作
					toNoticeCloud.setIp(an.getIp());
					toNoticeCloud.setIsPaid((byte) 1);// 已付款
					toNoticeCloud.setMoneyType((short) 0);// 商币类型
					toNoticeCloud.setCreateTime(new Date());
					// 在查
					Account downToAccInfoCloud = daoAcc.selectByPrimaryKey(toAccountId);
					toNoticeCloud.setMoneyDown(downToAccInfoCloud.getMoney()); // 结存现金余额
					toNoticeCloud.setCloudMoneyDown(downToAccInfoCloud.getCloudMoney()); // 结存商币余额

					res = daoNotice.insert(toNoticeCloud);
				}

				// 改游戏规则 判断 是不是消费行为即 收款方是不是商家
				// 收款方是商家 让进入推荐返币和合伙人返币
				if (toUserInfo.getIsMerchant() == 1) {
					// 给上线推荐奖 和 合伙人返
					serRecomm.transferByOnline(userid, toUserid, money, sn);
				}

				AccountNotice douAn = new AccountNotice();
				douAn.setSn(sn);
				douAn = daoNotice.selectBySn(an);
				AccountLock.unLock(an.getAccountId(), an.getUserid());
				if (an.getIsPaid() == 1) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return -4;
				}
			}

		} else {
			return -5;
		}

		return res;
	}

	@Override
	@Transactional
	public Integer financeRechargeWebHook(String sn, String outerSn, String resultCode) {
		log.info("成功处理start");
		Integer res = -1;
		AccountNotice an = new AccountNotice();
		an.setSn(sn);
		an = daoNotice.selectBySn(an);
		if (an == null || an.equals("")) {
			log.error("webHook_error:流水号数据库里没有[" + sn + "]");
			return -1;
		}
		if (an.getIsPaid() == 1) {
			log.error("webHook_info:数据库已经是isPaid[" + sn + "]");
			return 2;
		}
		boolean isLock1 = AccountLock.getLock(an.getAccountId(), an.getUserid());
		if (!isLock1) {
			return -11;
		}
		// 修改notice状态
		AccountNotice upAccNotice = new AccountNotice();
		upAccNotice.setOuterSn(outerSn);
		log.info("setIsPaid");
		upAccNotice.setIsPaid(new Byte("1"));
		upAccNotice.setOuterTime(new Date());
		upAccNotice.setId(an.getId());
		res = daoNotice.updateByPrimaryKeySelective(upAccNotice);
		log.info("notice update finish");
		if (res > 0) {

			Payment upPaymentTotal = new Payment();
			upPaymentTotal.setId(an.getPaymentId());
			upPaymentTotal.setTotalAmount(an.getMoney());
			daoPay.updatePaymentTotal(upPaymentTotal);
			User userInfo = daoUser.selectByPrimaryKey(an.getUserid());
			if (userInfo == null) {
				AccountLock.unLock(an.getAccountId(), an.getUserid());
				return -2;
			}
			Account accInfo = daoAcc.getUserAccount(userInfo.getId());
			Integer accountId = accInfo.getId();
			Integer userid = userInfo.getId();

			// 账户初使化
			if (accountId == null) {
				accountId = daoAcc.initUserAcount(userid);
			}
			BigDecimal money = an.getMoney();
			// 加钱
			if (money != null) {

				String ip = "111";
				// 账户变动前记录日志
				daoAccLog.addLog(accountId, sn, ip);
				res = daoAcc.addMoney(accountId, userid, money);

				// 新复制一条记录 ,先判断是不是最后一条记录
				AccountNotice checkIsLast = new AccountNotice();
				checkIsLast.setId(an.getId());
				checkIsLast.setUserid(an.getUserid());
				AccountNotice checkRes = daoNotice.checkLastById(checkIsLast);
				if (checkRes == null) {
					// 修改notice 结存
					AccountNotice downAccNotice = new AccountNotice();
					Account downAccInfo = daoAcc.selectByPrimaryKey(accountId);
					downAccNotice.setMoneyDown(downAccInfo.getMoney()); // 结存现金余额
					downAccNotice.setCloudMoneyDown(downAccInfo.getCloudMoney()); // 结存商币余额
					downAccNotice.setId(an.getId());
					res = daoNotice.updateByPrimaryKeySelective(downAccNotice);
				} else {
					AccountNotice upOldAn = new AccountNotice();
					upOldAn.setId(an.getId());
					upOldAn.setSn(an.getSn() + "_old");
					upOldAn.setIsPaid(new Byte("-1")); // 老数据 isPaid是-1
					daoNotice.updateByPrimaryKeySelective(upOldAn);
					String originSn = an.getSn();
					AccountNotice newAn = daoNotice.selectByPrimaryKey(an.getId());
					newAn.setId(null);
					newAn.setSn(originSn);
					newAn.setIsPaid(new Byte("1"));
					Account downAccInfo = daoAcc.selectByPrimaryKey(accountId);
					newAn.setMoneyDown(downAccInfo.getMoney()); // 结存现金余额
					newAn.setCloudMoneyDown(downAccInfo.getCloudMoney()); // 结存商币余额
					res = daoNotice.insertSelective(newAn);

				}
				AccountNotice douAn = new AccountNotice();
				douAn.setSn(sn);
				douAn = daoNotice.selectBySn(an);
				if (an.getIsPaid() == 1) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    AccountLock.unLock(an.getAccountId(), an.getUserid());
					return -3;
				}

			}

		} else {
			AccountLock.unLock(an.getAccountId(), an.getUserid());
			return -4;
		}
		AccountLock.unLock(an.getAccountId(), an.getUserid());
		return res;
	}

	@Override
	@Transactional
	public Integer financeUpgradeMemberWebHook(String sn, String outerSn, String resultCode) {

		Integer res = -1;
		AccountNotice an = new AccountNotice();
		an.setSn(sn);
		an = daoNotice.selectBySn(an);
		if (an == null || an.equals("")) {
			log.error("ERROR_IMP::会员升级流水号不存在[" + sn + "]");
			return -1;
		}
		if (an.getIsPaid() == 1) {
			log.error("ERROR_IMP::已经支付过了会员升级[" + sn + "]");
			return 2;
		}
		boolean isLock1 = AccountLock.getLock(an.getAccountId(), an.getUserid());
		if (!isLock1) {
			return -11;
		}
		// 修改notice状态
		AccountNotice upAccNotice = new AccountNotice();
		upAccNotice.setOuterSn(outerSn);
		upAccNotice.setIsPaid(new Byte("1"));
		upAccNotice.setOuterTime(new Date());
		upAccNotice.setId(an.getId());
		res = daoNotice.updateByPrimaryKeySelective(upAccNotice);
		if (res > 0) {
			Payment upPaymentTotal = new Payment();
			upPaymentTotal.setId(an.getPaymentId());
			upPaymentTotal.setTotalAmount(an.getMoney());
			daoPay.updatePaymentTotal(upPaymentTotal);
			User userInfo = daoUser.selectByPrimaryKey(an.getUserid());
			if (userInfo == null) {
				log.error("ERROR_IMP::会员升级但用户不存在[" + sn + "]");
				return -3;
			}
			Account accInfo = daoAcc.getUserAccount(userInfo.getId());
			Integer accountId = accInfo.getId();
			Integer userid = userInfo.getId();

			// 账户初使化
			if (accountId == null) {
				accountId = daoAcc.initUserAcount(userid);
			}
			BigDecimal money = an.getMoney();
			// 加钱
			if (money != null) {

				// 会员等级
				String level = "comm";
				if (money.compareTo(new BigDecimal("100")) == 0) {
					level = "vip";
				} else if (money.compareTo(new BigDecimal("1000")) == 0) {
					level = "gold";
				}
				if (userInfo.getLevel().equals(level)) {
					log.error("ERROR_IMP::会员升级已经是" + level + "[" + sn + "]");
					return -4;
				}
				String ip = an.getIp();
				// 账户变动前记录日志
				daoAccLog.addLog(accountId, sn, ip);
				res = daoAcc.addMoney(accountId, userid, money); // money为正数

				AccountNotice checkIsLast = new AccountNotice();
				checkIsLast.setId(an.getId());
				checkIsLast.setUserid(an.getUserid());
				AccountNotice checkRes = daoNotice.checkLastById(checkIsLast);

				if (checkRes == null) {
					// 修改notice 结存
					AccountNotice downAccNotice = new AccountNotice();
					Account downAccInfo = daoAcc.selectByPrimaryKey(accountId);
					downAccNotice.setMoneyDown(downAccInfo.getMoney()); // 结存现金余额
					downAccNotice.setCloudMoneyDown(downAccInfo.getCloudMoney()); // 结存商币余额
					downAccNotice.setId(an.getId());

					res = daoNotice.updateByPrimaryKeySelective(downAccNotice);
				} else {
					String originSn = an.getSn();
					AccountNotice upOldAn = new AccountNotice();
					upOldAn.setId(an.getId());
					upOldAn.setSn(an.getSn() + "_old");
					upOldAn.setIsPaid(new Byte("-1")); // 老数据 isPaid是-1
					daoNotice.updateByPrimaryKeySelective(upOldAn);

					AccountNotice newAn = daoNotice.selectByPrimaryKey(an.getId());
					newAn.setId(null);
					newAn.setSn(originSn);
					newAn.setIsPaid(new Byte("1"));
					Account downAccInfo = daoAcc.selectByPrimaryKey(accountId);
					newAn.setMoneyDown(downAccInfo.getMoney()); // 结存现金余额
					newAn.setCloudMoneyDown(downAccInfo.getCloudMoney()); // 结存商币余额
					res = daoNotice.insertSelective(newAn);
				}
				// 在操作扣款
				// 扣现金
				res = daoAcc.addMoney(accountId, userid, money.multiply(new BigDecimal("-1")));
				AccountNotice notice = new AccountNotice();
				notice.setAccountId(accountId);
				notice.setUserid(userid);
				notice.setMoney(money.multiply(new BigDecimal("-1")));// 现金负数
				notice.setTradeType(4); // 类型4 升级
				String snTemp = PrimaryGenerater.getInstance().next();
				notice.setCsn(sn);// 这笔转账动作流水号
				notice.setSn(snTemp);// 唯一流水号
				notice.setOuterTime(new Date());

				notice.setTitle(String.format("升级为%s -%s", level, money));
				notice.setPaymentId(an.getPaymentId());// 余额支付 4-Balance
				notice.setIp(an.getIp());
				notice.setIsPaid((byte) 1);// 已付款
				notice.setMoneyType((short) 1);// 现金类型1,
				notice.setCreateTime(new Date());
				// 在查
				Account downAccInfo = daoAcc.selectByPrimaryKey(accountId);
				notice.setMoneyDown(downAccInfo.getMoney()); // 结存现金余额
				notice.setCloudMoneyDown(downAccInfo.getCloudMoney()); // 结存商币余额
				res = daoNotice.insert(notice);

				res = daoAcc.addCloudMoney(accountId, userid, money);
				AccountNotice noticeCloud = new AccountNotice();
				noticeCloud.setAccountId(accountId);
				noticeCloud.setUserid(userid);
				noticeCloud.setCloudMoney(money);// 商币

				noticeCloud.setTradeType(new Integer("4")); // 4 会员升级
				noticeCloud.setSn(sn);// 流水号
				snTemp = PrimaryGenerater.getInstance().next();
				noticeCloud.setCsn(sn);// 这笔转账动作流水号
				noticeCloud.setSn(snTemp);// 唯一流水号
				// noticeCloud.setOperator((int) -1); // -1系统操作
				noticeCloud.setOuterTime(new Date());

				noticeCloud.setTitle(String.format("会员升级得商币+%s", money));
				noticeCloud.setPaymentId(3);// 3 SysTransfer 系统操作
				noticeCloud.setIp(an.getIp());
				noticeCloud.setIsPaid((byte) 1);// 已付款
				noticeCloud.setMoneyType((short) 0);// 商币类型0,
				noticeCloud.setCreateTime(new Date());
				// 在查
				Account downAccInfoCloud = daoAcc.selectByPrimaryKey(accountId);
				noticeCloud.setMoneyDown(downAccInfoCloud.getMoney()); // 结存现金余额
				noticeCloud.setCloudMoneyDown(downAccInfoCloud.getCloudMoney()); // 结存商币余额
				res = daoNotice.insert(noticeCloud);

				// User userLastCheck = daoUser.selectByPrimaryKey(userid);
				// if(userLastCheck.getLevel().equals(level)){
				// //主动回滚
				// log.error("ERROR_IMP:在线支付升级主动回滚["+userInfo.getName()+"]"+level+"||"+userLastCheck.getLevel());
				// TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				// return -6;
				// }

				// 更新用户等级
				User record = new User();
				record.setLevel(level);
				record.setId(userid);
				res = daoUser.updateByPrimaryKeySelective(record);
				// 推荐人返现
				serRecomm.memberUpgradeByOnline(level, userid, money, sn);
				// add cloud,update notice,update user 推荐人
				AccountLock.unLock(an.getAccountId(), an.getUserid());
			}

		} else {
			AccountLock.unLock(an.getAccountId(), an.getUserid());
			return -5;
		}

		return res;
	}

	@Override
	public DtoTransferBalance financeTransferOnline(String userName, String toUserName, BigDecimal money, String ip,
			String memo, String paymentType) {
		Integer res = -1;
		DtoTransferBalance result = new DtoTransferBalance();
		result.setRes(res);
		if (money == null || money.compareTo(new BigDecimal("0.00")) == 0
				|| money.compareTo(new BigDecimal("0.00")) == -1) {
			result.setRes(new Integer("-1"));
			return result;
		}

		User userInfo = daoUser.selectByName(userName);
		User toUserInfo = daoUser.selectByName(toUserName);
		if (userInfo == null || toUserInfo == null) {
			result.setRes(new Integer("-1"));
			return result;
		}

		Account accInfo = daoAcc.getUserAccount(userInfo.getId());
		Account toAccInfo = daoAcc.getUserAccount(toUserInfo.getId());

		Integer accountId = accInfo.getId();
		Integer userid = userInfo.getId();

		Integer toAccountId = toAccInfo.getId();
		Integer toUserid = toUserInfo.getId();

		// 账户初使化
		if (accountId == null) {
			accountId = daoAcc.initUserAcount(userid);
		}
		// 账户初使化
		if (toAccountId == null) {
			toAccountId = daoAcc.initUserAcount(toUserid);
		}

		// 没有查询到用户的帐号,退出
		if (daoAcc.selectByPrimaryKey(accountId) == null) {
			result.setRes(new Integer("-1"));
			return result;
		}
		Payment payment = daoPay.getPaymentByClassName(paymentType);
		if (payment == null) {
			result.setRes(new Integer("-1"));
			result.setMessage("支付方式不正确");
			return result;
		}

		// 加钱
		if (money != null) {
			String sn = PrimaryGenerater.getInstance().next();

			// 转出者
			// 写资金日志明细 转出者
			AccountNotice notice = new AccountNotice();
			notice.setAccountId(accountId);
			notice.setUserid(userid);
			notice.setMoney(money);// 现金正数

			notice.setTradeType(16); // 在线转帐
			notice.setSn(sn);// 唯一流水号

			// desc 32字节，一个汉字2个字节
			String desc = String.format("转账给%s,-%s元", toUserInfo.getName(), money);
			// 转账业务是 前加余额在减
			String title = String.format(payment.getName() + "转入余额+%s元", money);
			notice.setTitle(title);
			notice.setMemo(memo);
			notice.setPaymentId(payment.getId());
			notice.setIp(ip);
			notice.setIsPaid((byte) 0);// 未付，默认0
			notice.setMoneyType((short) 1);// 现金类型
			notice.setCreateTime(new Date());
			notice.setToUserid(toUserid);
			// 在查
			Account downAccInfo = daoAcc.selectByPrimaryKey(accountId);
			notice.setMoneyDown(downAccInfo.getMoney()); // 结存现金余额
			notice.setCloudMoneyDown(downAccInfo.getCloudMoney()); // 结存商币余额
			res = daoNotice.insert(notice);
			result.setRes(res);
			result.setSn(sn);
			result.setDesc(desc);
			result.setMoney(money);
			result.setPaymentType(paymentType);
		}

		return result;
	}

	@Override
	public Account selectByPrimaryKey(Integer id) {
		return daoAcc.selectByPrimaryKey(id);
	}

	/**
	 * 成为合伙人加商币
	 * 
	 * @param accChangeInfo
	 *            用户账户变动信息
	 * @param adminId
	 *            操作管理员id
	 * @return >0成功,其它这失败
	 */
	@Override
	public Integer partnerNewAddCloudMoney(int partnerid, String ip) {
		Integer res = -1;

		// 查询用户对应级别的升级费
		Partner part = daoPart.selectByPrimaryKey(partnerid);
		int userId = part.getUserid();

		Account accInfo = daoAcc.getUserAccount(userId);
		Integer accountId = accInfo.getId();
		Integer userid = accInfo.getUserid();

		// 账户初使化
		if (accountId == null) {
			accountId = daoAcc.initUserAcount(userid);
		}

		// 没有查询到用户的帐号,退出
		if (daoAcc.selectByPrimaryKey(accountId) == null)
			return -1;

		// 读取用户账户信息
		UserAccountInfo currAccInfo = this.getUserAccountInfo(userid);

		PartnerLevel partLevel = daoPartnerLevel.getLevelOfPartner(partnerid);
		if (partLevel != null && partLevel.getValue() != null) {

			// 升级合伙人交的费用,10000表示单位为:万元
			BigDecimal fee = partLevel.getValue().multiply(new BigDecimal("10000")); 

			String sn = PrimaryGenerater.getInstance().next();

			// 账户变动前记录日志
			daoAccLog.addLog(accountId, sn, ip);

			// 加商币
			res = daoAcc.addCloudMoney(accountId, userid, fee);

			// 读取用户账户信息
			UserAccountInfo endAccInfo = this.getUserAccountInfo(userid);

			// 写商币日志明细
			AccountNotice notice = new AccountNotice();
			notice.setAccountId(accountId);
			notice.setUserid(userid);
			notice.setCloudMoney(fee);// 商币

			notice.setTradeType(21); // 赠送商币
			notice.setSn(sn);// 流水号
			notice.setOuterTime(new Date());
			notice.setOperator(-1);// 操作人为系统

			notice.setTitle(String.format("成为%s,赠送: %s 商币", partLevel.getName(), fee));
			notice.setMemo(String.format("成为%s,赠送: %s 商币", partLevel.getName(), fee));

			notice.setPaymentId(1);// 管理员充值类型
			notice.setIp(ip);
			notice.setIsPaid((byte) 1);// 已付款
			notice.setMoneyType((short) 0);// 商币类型
			notice.setCreateTime(new Date());
			notice.setMoneyDown(endAccInfo.getMoney()); // 结存现金余额
			notice.setCloudMoneyDown(endAccInfo.getCloudMoney()); // 结存商币余额
			daoNotice.insert(notice);
		}

		return res;
	}

	/**
	 * 账户列表
	 */
	@Override
	public List<Map<String, Object>> getPage(Pager pager, UserListQryDto qryParam) {
		StringBuilder where = new StringBuilder();
		if (qryParam != null) {
			if (!StringUtils.isEmpty(qryParam.getId())) {
				where.append(String.format(" and u.id=%s ", qryParam.getId()));
			}
			if (!StringUtils.isEmpty(qryParam.getLevel()) && !"-1".equals(qryParam.getLevel())) {
				where.append(String.format(" and u.level='%s' ", qryParam.getLevel().trim()));
			}
			if (!StringUtils.isEmpty(qryParam.getName())) {
				where.append(String.format(" and u.name='%s' ", qryParam.getName().trim()));
			}
			if (!StringUtils.isEmpty(qryParam.getMobile())) {
				where.append(String.format(" and u.mobile='%s' ", qryParam.getMobile().trim()));
			}
			if (!StringUtils.isEmpty(qryParam.getCreateTime()) && StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and u.create_time >='%s' ", qryParam.getCreateTime().trim()));
			} else if (StringUtils.isEmpty(qryParam.getCreateTime())
					&& !StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and u.create_time <='%s 23:59:59' ", qryParam.getCreateTime1().trim()));
			} else if (!StringUtils.isEmpty(qryParam.getCreateTime())
					&& !StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and (u.create_time between '%s' and '%s 23:59 23:59:59') ",
						qryParam.getCreateTime().trim(), qryParam.getCreateTime1().trim()));
			}
			
			if (!StringUtils.isEmpty(qryParam.getMoneyMin()) && StringUtils.isEmpty(qryParam.getMoneyMax())) {
				where.append(String.format(" and a.money >=%d ", qryParam.getMoneyMin()));
			} else if (StringUtils.isEmpty(qryParam.getMoneyMin())
					&& !StringUtils.isEmpty(qryParam.getMoneyMax())) {
				where.append(String.format(" and a.money <=%d ", qryParam.getMoneyMax()));
			} else if (!StringUtils.isEmpty(qryParam.getMoneyMin())
					&& !StringUtils.isEmpty(qryParam.getMoneyMax())) {
				where.append(String.format(" and (a.money between %d  and %d ) ",
						qryParam.getMoneyMin(), qryParam.getMoneyMax()));
			}
			
			if (!StringUtils.isEmpty(qryParam.getCloudMin()) && StringUtils.isEmpty(qryParam.getCloudMax())) {
				where.append(String.format(" and a.cloud_money >=%d ", qryParam.getCloudMin()));
			} else if (StringUtils.isEmpty(qryParam.getCloudMin())
					&& !StringUtils.isEmpty(qryParam.getCloudMax())) {
				where.append(String.format(" and a.cloud_money <=%d ", qryParam.getMoneyMax()));
			} else if (!StringUtils.isEmpty(qryParam.getCloudMin())
					&& !StringUtils.isEmpty(qryParam.getCloudMax())) {
				where.append(String.format(" and (a.cloud_money between %d  and %d ) ",
						qryParam.getCloudMin(), qryParam.getCloudMax()));
			}
			
		}
		StringBuilder order = new StringBuilder();
		if(qryParam.getSort()!=null){
			switch (qryParam.getSort()) {
				case 0:	
					order.append(" a.money asc ");
					break;
				case 1:
					order.append(" a.money desc ");
					break;
				case 2:			
					order.append(" a.cloud_money asc ");
					break;
				case 3:			
					order.append(" a.cloud_money desc ");
					break;
			}
		}
		
		List<Map<String, Object>> list = daoAcc.getPage(pager, where.toString(),order.toString());
		return list;
	}

	/**
	 * 升级合伙人加商币
	 * @param partnerid 合伙人id 
	 * @param upLevel   升级后级别
	 * @param ip        ip
	 * @return
	 */
	@Override
	public Integer partnerUpgradeAddCloudMoney(int partnerid,int upLevel, String ip) {
		Integer res = -1;

		// 查询用户对应级别的升级费
		Partner part = daoPart.selectByPrimaryKey(partnerid);
		int userId = part.getUserid();

		Account accInfo = daoAcc.getUserAccount(userId);
		Integer accountId = accInfo.getId();
		Integer userid = accInfo.getUserid();

		// 账户初使化
		if (accountId == null) {
			accountId = daoAcc.initUserAcount(userid);
		}

		// 没有查询到用户的帐号,退出
		if (daoAcc.selectByPrimaryKey(accountId) == null)
			return -1;

		// 读取用户账户信息
		UserAccountInfo currAccInfo = this.getUserAccountInfo(userid);

		PartnerLevel partLevel = daoPartnerLevel.getLevelOfPartner(partnerid);
		PartnerLevel upPartLevel = daoPartnerLevel.selectByPrimaryKey(upLevel);
		
		if (partLevel != null && partLevel.getValue() != null) {

			// 升级合伙人交的费用,10000表示单位为:万元
			BigDecimal fee1 = partLevel.getValue().multiply(new BigDecimal("10000"));
			BigDecimal fee2 = upPartLevel.getValue().multiply(new BigDecimal("10000"));
			
			//升级后费用必须大于升级前
			if(fee2.compareTo(fee1)<=0){
				return -1;
			}
			
			//升级差价
			BigDecimal fee = fee2.subtract(fee1);		
            
			
			String sn = PrimaryGenerater.getInstance().next();

			// 账户变动前记录日志
			daoAccLog.addLog(accountId, sn, ip);

			// 加商币
			res = daoAcc.addCloudMoney(accountId, userid, fee);

			// 读取用户账户信息
			UserAccountInfo endAccInfo = this.getUserAccountInfo(userid);

			// 写商币日志明细
			AccountNotice notice = new AccountNotice();
			notice.setAccountId(accountId);
			notice.setUserid(userid);
			notice.setCloudMoney(fee);// 商币

			notice.setTradeType(21); // 赠送商币
			notice.setSn(sn);// 流水号
			notice.setOuterTime(new Date());
			notice.setOperator(-1);// 操作人为系统

			notice.setTitle(String.format("升级为%s,赠送: %s 商币", upPartLevel.getName(), fee));
			notice.setMemo(String.format("升级为%s,赠送: %s 商币", upPartLevel.getName(), fee));

			notice.setPaymentId(1);// 管理员充值类型
			notice.setIp(ip);
			notice.setIsPaid((byte) 1);// 已付款
			notice.setMoneyType((short) 0);// 商币类型
			notice.setCreateTime(new Date());
			notice.setMoneyDown(endAccInfo.getMoney()); // 结存现金余额
			notice.setCloudMoneyDown(endAccInfo.getCloudMoney()); // 结存商币余额
			daoNotice.insert(notice);
		}

		return res;
	}

	@Override
	public List<Map<String, Object>> getAccountListExport( UserListQryDto qryParam) {
		StringBuilder where=new StringBuilder();
		if (qryParam != null) {
			if (!StringUtils.isEmpty(qryParam.getId())) {
				where.append(String.format(" and u.id=%s ", qryParam.getId()));
			}
			if (!StringUtils.isEmpty(qryParam.getLevel()) && !"-1".equals(qryParam.getLevel())) {
				where.append(String.format(" and u.level='%s' ", qryParam.getLevel().trim()));
			}
			if (!StringUtils.isEmpty(qryParam.getName())) {
				where.append(String.format(" and u.name='%s' ", qryParam.getName().trim()));
			}
			if (!StringUtils.isEmpty(qryParam.getMobile())) {
				where.append(String.format(" and u.mobile='%s' ", qryParam.getMobile().trim()));
			}
			if (!StringUtils.isEmpty(qryParam.getCreateTime()) && StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and u.create_time >='%s' ", qryParam.getCreateTime().trim()));
			} else if (StringUtils.isEmpty(qryParam.getCreateTime())
					&& !StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and u.create_time <='%s 23:59:59' ", qryParam.getCreateTime1().trim()));
			} else if (!StringUtils.isEmpty(qryParam.getCreateTime())
					&& !StringUtils.isEmpty(qryParam.getCreateTime1())) {
				where.append(String.format(" and (u.create_time between '%s' and '%s 23:59 23:59:59') ",
						qryParam.getCreateTime().trim(), qryParam.getCreateTime1().trim()));
			}
			
			if (!StringUtils.isEmpty(qryParam.getMoneyMin()) && StringUtils.isEmpty(qryParam.getMoneyMax())) {
				where.append(String.format(" and a.money >=%d ", qryParam.getMoneyMin()));
			} else if (StringUtils.isEmpty(qryParam.getMoneyMin())
					&& !StringUtils.isEmpty(qryParam.getMoneyMax())) {
				where.append(String.format(" and a.money <=%d ", qryParam.getMoneyMax()));
			} else if (!StringUtils.isEmpty(qryParam.getMoneyMin())
					&& !StringUtils.isEmpty(qryParam.getMoneyMax())) {
				where.append(String.format(" and (a.money between %d  and %d ) ",
						qryParam.getMoneyMin(), qryParam.getMoneyMax()));
			}
			
			if (!StringUtils.isEmpty(qryParam.getCloudMin()) && StringUtils.isEmpty(qryParam.getCloudMax())) {
				where.append(String.format(" and a.cloud_money >=%d ", qryParam.getCloudMin()));
			} else if (StringUtils.isEmpty(qryParam.getCloudMin())
					&& !StringUtils.isEmpty(qryParam.getCloudMax())) {
				where.append(String.format(" and a.cloud_money <=%d ", qryParam.getMoneyMax()));
			} else if (!StringUtils.isEmpty(qryParam.getCloudMin())
					&& !StringUtils.isEmpty(qryParam.getCloudMax())) {
				where.append(String.format(" and (a.cloud_money between %d  and %d ) ",
						qryParam.getCloudMin(), qryParam.getCloudMax()));
			}
			
		}
		
		StringBuilder order = new StringBuilder();
		if(qryParam.getSort()!=null){
			switch (qryParam.getSort()) {
				case 0:	
					order.append(" a.money asc ");
					break;
				case 1:
					order.append(" a.money desc ");
					break;
				case 2:			
					order.append(" a.cloud_money asc ");
					break;
				case 3:			
					order.append(" a.cloud_money desc ");
					break;
			}
		}
		
		
		List<Map<String, Object>> list = daoAcc.getAccountListExport(where.toString(),order.toString());
		return list;
	}
}
